package cn.lg.soar.system.biz.modules.general.entity;

import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.base.entity.IBaseEntity;
import cn.lg.soar.database.translate.TranslateByEntity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Objects;


/**
 * 文件元数据表
 * @author luguoxiang
 * @since 2024-06-01
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Data
@Schema(description = "文件元数据表")
@TableName("lg_sys_file_metadata")
public class FileMetadata {

    /**  */
    @NotNull(message = "主键不能为空", groups = { Update.class })
    @ExcelIgnore
    @Schema(description = "主键")
    @TableId
    private Long id;


    /** 文件编码（文件唯一校验） */
    @NotBlank(message = "文件编码（文件唯一校验）不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "文件编码（文件唯一校验）")
    @Schema(description = "文件编码（文件唯一校验）")
    private String code;

    /** 文件仓库id */
    @TranslateByEntity(FileStorage.class)
    @NotNull(message = "文件仓库id不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "文件仓库id")
    @Schema(description = "文件仓库id")
    private Integer storageId;

    /** 目录 */
    @NotBlank(message = "目录不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "目录")
    @Schema(description = "目录")
    private String directory;

    /** 后缀 */
    @NotBlank(message = "后缀不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "后缀")
    @Schema(description = "后缀")
    private String suffix;

    /** 文件大小 */
    @NotNull(message = "文件大小不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "文件大小")
    @Schema(description = "文件大小")
    private Long size;

    /** 原文件名称（第一次上传名称） */
    @NotBlank(message = "原文件名称（第一次上传名称）不能为空", groups = { Create.class, Update.class })
    @ExcelProperty(value = "原文件名称（第一次上传名称）")
    @Schema(description = "原文件名称（第一次上传名称）")
    private String originalName;

    /** 创建人 */
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @TableField(value = IBaseEntity.CREATE_BY, fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER)
    @Schema(description = "创建人")
    private Long createBy;

    /** 上传时间 */
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "创建时间")
    @TableField(value = IBaseEntity.CREATE_TIME, fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    /**
     * 存储名称
     * @return
     */
    public String filename() {
        return directory + "/" + id +"."+ suffix;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FileMetadata that = (FileMetadata) o;
        return Objects.equals(code, that.code);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code);
    }
}
